package com.kovell.cairngormlite
{
	import com.adobe.cairngorm.business.Responder;
	
	import mx.controls.Alert;
	import mx.rpc.AbstractOperation;
	import mx.rpc.AsyncToken;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;

	public class CairngormliteDelegate
	{
		
		private var responder:Responder;
		private var remoteObject:RemoteObject;
		
		public function CairngormliteDelegate(responder:Responder, serviceId:String)
		{
			this.responder = responder;			
			this.remoteObject = ServiceLocator.getInstance().getRemoteObject(serviceId);
		}
		
		public function invoke(method:String, ...args):void{
			
			var op:AbstractOperation = remoteObject.getOperation(method);
			op.arguments = args.toString().split(",");
			var token:AsyncToken = op.send();
			var resp:mx.rpc.Responder = new mx.rpc.Responder(resultHandler, faultHandler);
			token.addResponder(resp);
		}
		
		/**
		 *  resultHandler.
		 * 
		 *  This method will trigger if result was response from server
		 * */
		
		public function resultHandler(event:ResultEvent):void{
			responder.onResult(event);	
		}
		
		/**
		 * faultHandler.
		 * 
		 * This method will trigger if fault is occur
		 * */
		
		public function faultHandler(event:FaultEvent):void{
			responder.onFault(event);
		}
	}
}